home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / VBSamples / Demos / AirHockey / cCamera.cls < prev    next >
Encoding:
Visual Basic class definition  |  2001-10-08  |  6.8 KB  |  214 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "cCamera"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = False
  14. Option Explicit
  15.  
  16. 'Here we will encapsulate all of the code needed for the camera
  17.  
  18. Private Enum DefaultCameraViews
  19.     DefaultView
  20.     OverHeadView
  21.     SideOverheadView1
  22.     SideOverheadView2
  23.     OpponentView
  24.     CustomView
  25. End Enum
  26.  
  27. 'Here are the constants for the default view
  28. Private Const mnDefaultX As Single = 0
  29. Private Const mnDefaultY As Single = 10
  30. Private Const mnDefaultZ As Single = -25
  31. 'Here are the constants for the overhead views
  32. Private Const mnOverheadX As Single = 0
  33. Private Const mnOverheadY As Single = 28
  34. Private Const mnOverheadZ As Single = -1
  35. 'Here are the constants for the side overhead views
  36. Private Const mnSide1X As Single = 25
  37. Private Const mnSide1Y As Single = 12.5
  38. Private Const mnSide1Z As Single = 0
  39. Private Const mnSide2X As Single = -25
  40. Private Const mnSide2Y As Single = 12.5
  41. Private Const mnSide2Z As Single = 0
  42. 'Here are the constants for the opponent views
  43. Private Const mnOpponentX As Single = 0
  44. Private Const mnOpponentY As Single = 10
  45. Private Const mnOpponentZ As Single = 25
  46.  
  47. 'Local variables for the properties of the puck
  48. Private moPosition As D3DVECTOR 'Current position of the camera
  49. Private moVelocity As D3DVECTOR 'Current velocity of the camera
  50. Private moDest As D3DVECTOR 'Destination of the camera
  51. Private mlCameraTime As Long 'Last time the puck was updated
  52. Private moLastPosition As D3DVECTOR 'Last position of the camera
  53. 'The default camera views
  54. Public CameraView As Long
  55.  
  56. 'Position property
  57. Public Property Let Position(oPos As D3DVECTOR)
  58.     moPosition = oPos
  59. End Property
  60.  
  61. Public Property Get Position() As D3DVECTOR
  62.     Position = moPosition
  63. End Property
  64.  
  65. 'Velocity property
  66. Public Property Let Velocity(oVel As D3DVECTOR)
  67.     moVelocity = oVel
  68. End Property
  69.  
  70. Public Property Get Velocity() As D3DVECTOR
  71.     Velocity = moVelocity
  72. End Property
  73.  
  74. 'LastPosition prop
  75. Public Property Let LastPosition(oLastPos As D3DVECTOR)
  76.     moLastPosition = oLastPos
  77. End Property
  78.  
  79. Public Property Get LastPosition() As D3DVECTOR
  80.     LastPosition = moLastPosition
  81. End Property
  82.  
  83. 'Dest property
  84. Public Property Let Dest(oPos As D3DVECTOR)
  85.     moDest = oPos
  86. End Property
  87.  
  88. Public Property Get Dest() As D3DVECTOR
  89.     Dest = moDest
  90. End Property
  91.  
  92. 'Methods
  93. Public Sub UpdatePosition()
  94.     Dim RealVelocity As D3DVECTOR
  95.     Dim DistancePointX As Single
  96.     Dim DistancePointY As Single
  97.     Dim DistancePointZ As Single
  98.     
  99.     'Here we will update the position of the camera
  100.     'and move it based on the velocity assigned.
  101.     If mlCameraTime = 0 Then mlCameraTime = timeGetTime
  102.     'First calculate the 'real' velocity (based on the time)
  103.     RealVelocity.x = ((timeGetTime - mlCameraTime) / 100) * moVelocity.x
  104.     RealVelocity.y = ((timeGetTime - mlCameraTime) / 100) * moVelocity.y
  105.     RealVelocity.z = ((timeGetTime - mlCameraTime) / 100) * moVelocity.z
  106.     'Let's save our current position
  107.     moLastPosition = moPosition
  108.     'Now let's see if moving our position will move us past our destination
  109.     'if it does, move us to our destination
  110.     
  111.     'First check the X axis
  112.     DistancePointX = Sqr((moDest.x - moPosition.x) * (moDest.x - moPosition.x))
  113.     If DistancePointX < RealVelocity.x Then
  114.         moPosition.x = moDest.x 'We've arrived
  115.         moVelocity.x = 0
  116.     Else
  117.         moPosition.x = moPosition.x + RealVelocity.x 'We haven't got to our destination yet, keep going
  118.     End If
  119.     'Now check the Y axis
  120.     DistancePointY = Sqr((moDest.y - moPosition.y) * (moDest.y - moPosition.y))
  121.     If DistancePointY < RealVelocity.y Then
  122.         moPosition.y = moDest.y 'We've arrived
  123.         moVelocity.y = 0
  124.     Else
  125.         moPosition.y = moPosition.y + RealVelocity.y 'We haven't got to our destination yet, keep going
  126.     End If
  127.     'Now check the Z axis
  128.     DistancePointZ = Sqr((moDest.z - moPosition.z) * (moDest.z - moPosition.z))
  129.     If DistancePointZ < RealVelocity.z Then
  130.         moPosition.z = moDest.z 'We've arrived
  131.         moVelocity.z = 0
  132.     Else
  133.         moPosition.z = moPosition.z + RealVelocity.z 'We haven't got to our destination yet, keep going
  134.     End If
  135.     'Make sure our velocity is going in the right direction
  136.     If DistancePointX < Sqr((moDest.x - moPosition.x) * (moDest.x - moPosition.x)) Then
  137.         'It's not, reverse it
  138.         moVelocity.x = moVelocity.x * -1
  139.     End If
  140.     If DistancePointY < Sqr((moDest.y - moPosition.y) * (moDest.y - moPosition.y)) Then
  141.         'It's not, reverse it
  142.         moVelocity.y = moVelocity.y * -1
  143.     End If
  144.     If DistancePointZ < Sqr((moDest.z - moPosition.z) * (moDest.z - moPosition.z)) Then
  145.         'It's not, reverse it
  146.         moVelocity.z = moVelocity.z * -1
  147.     End If
  148.     mlCameraTime = timeGetTime
  149. End Sub
  150.  
  151. Public Sub NextCameraPosition(ByVal lPlayerID As Long)
  152.     If CameraView = CustomView Then
  153.         CameraView = DefaultView
  154.     Else
  155.         CameraView = CameraView + 1
  156.         If CameraView = CustomView Then
  157.             CameraView = DefaultView
  158.         End If
  159.     End If
  160.     UpdateToNewPosition lPlayerID
  161. End Sub
  162.  
  163. Public Sub SetCameraPosition(ByVal lCameraPos As Long, ByVal lPlayerID As Long)
  164.     CameraView = lCameraPos
  165.     If CameraView <> CustomView Then UpdateToNewPosition lPlayerID
  166. End Sub
  167.  
  168. Private Sub UpdateToNewPosition(ByVal lPlayerID As Long)
  169.     
  170.     Select Case CameraView
  171.     Case DefaultView
  172.         If lPlayerID = 0 Then
  173.             moDest.x = mnDefaultX
  174.             moDest.y = mnDefaultY
  175.             moDest.z = mnDefaultZ
  176.         Else 'Default view should be the opponents view
  177.             moDest.x = mnOpponentX
  178.             moDest.y = mnOpponentY
  179.             moDest.z = mnOpponentZ
  180.         End If
  181.     Case OpponentView
  182.         If lPlayerID = 1 Then
  183.             moDest.x = mnDefaultX
  184.             moDest.y = mnDefaultY
  185.             moDest.z = mnDefaultZ
  186.         Else 'Default view should be the opponents view
  187.             moDest.x = mnOpponentX
  188.             moDest.y = mnOpponentY
  189.             moDest.z = mnOpponentZ
  190.         End If
  191.     Case OverHeadView
  192.         moDest.x = mnOverheadX
  193.         moDest.y = mnOverheadY
  194.         moDest.z = mnOverheadZ
  195.     Case SideOverheadView1
  196.         moDest.x = mnSide1X
  197.         moDest.y = mnSide1Y
  198.         moDest.z = mnSide1Z
  199.     Case SideOverheadView2
  200.         moDest.x = mnSide2X
  201.         moDest.y = mnSide2Y
  202.         moDest.z = mnSide2Z
  203.     End Select
  204.     'Set up a default velocity
  205.     moVelocity.x = 3
  206.     moVelocity.y = 3
  207.     moVelocity.z = 3
  208.  
  209. End Sub
  210.  
  211. Private Sub Class_Initialize()
  212.     CameraView = DefaultView
  213. End Sub
  214.